package com.li.manage.action;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.li.manage.constant.Constants;
import com.li.manage.model.User;
import com.li.manage.service.UserService;

@Controller
@RequestMapping("/login")
public class LoginAction {

	@Autowired
	private UserService userService;
	
	@RequestMapping("/toLogin")
	public ModelAndView toLogin(HttpServletRequest req){
		ModelAndView mav = new ModelAndView("/login");
		return mav;
	}
	
	@RequestMapping("/login")
	public ModelAndView login(HttpServletRequest req,String loginName,String loginPassword){
		ModelAndView mav = new ModelAndView();
		System.out.println("--------------我是改变的");
		String returns = userService.login(loginName,loginPassword);
		if("success".equals(returns)){
			User user = userService.getUserByLoginName(loginName);
			req.getSession().setAttribute(Constants.USER_LOGIN_STR, user);
			mav.setViewName("redirect:/index");
		}else if("noUser".equals(returns)){
			mav.setViewName("/login");
			mav.addObject("loginNameError", "没有这个用户");
		}else if("passwordError".equals(returns)){
			mav.setViewName("/login");
			mav.addObject("loginPasswordError", "密码错误！");
		}else if("userTingYong".equals(returns)){
			mav.setViewName("/login");
			mav.addObject("loginNameError", "账号已停用");
		}
		return mav;
	}
}
